package xq.gwt.ui.crud;

import java.util.ArrayList;


import xq.gwt.mvc.model.EntityModel;
import xq.gwt.ui.widgets.entity.EntityPresenter;
import xq.gwt.ui.widgets.entity.EntitySelectedListener;
import xq.gwt.ui.widgets.factory.FormItemPropertyPresenterFactory;
import xq.gwt.ui.widgets.property.EntityListItemPropertyPresenter;
import xq.gwt.ui.widgets.property.FormItemPropertyPresenter;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.events.FormItemInitHandler;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.Layout;

public class EntitySelectorView extends DynamicForm implements EntityPresenter, EntitySelectedListener{	
	private FormItemPropertyPresenter[] propertyPresenters;	
	private CanvasItem headerItem;
	private EntitySelectedListener selectionListener;
	
	public EntitySelectorView(){
		super();
		init();		
	}
	
	public EntitySelectorView(Canvas header){
		super();
		init();
		if(header != null){
			headerItem = new CanvasItem();
			headerItem.setWidth("*");
			headerItem.setColSpan("*");	
			headerItem.setShowTitle(false);     
			headerItem.setStartRow(true);
			headerItem.setEndRow(true);
			headerItem.setCanvas(header);
		}
	}
	
	private void init(){
		addDrawHandler(new DrawHandler() {
			@Override
			public void onDraw(DrawEvent event) {
				if (propertyPresenters != null) {
					for (int i = 0; i < propertyPresenters.length; i++) {
						propertyPresenters[i].validate();
					}
				}
			}
		});
	}
	
	@Override
	public void setEntity(EntityModel entity) {
		if (entity == null )
			return;
		
		setTitle(entity.getTitle());
		propertyPresenters = FormItemPropertyPresenterFactory.buildPresenters(entity.getProperties());
		ArrayList<FormItem> formItemList = new ArrayList<FormItem>();
		if(headerItem != null){
			formItemList.add(headerItem);
		}
		
		for (int i = 0; i < propertyPresenters.length; i++) {
			formItemList.add(propertyPresenters[i].getFormItem());
			if(propertyPresenters[i] instanceof EntityListItemPropertyPresenter){
				((EntityListItemPropertyPresenter)propertyPresenters[i]).setEntitySelectedListener(this);
			}
		}
		
		IButton[] commandButtons = FormItemPropertyPresenterFactory.buildButtons(entity.getCommands());
		if(commandButtons != null){
			ButtonListItem buttonList = new ButtonListItem(commandButtons);
			formItemList.add(buttonList);			
		}
		
		
		FormItem[] formItems = new FormItem[formItemList.size()];
		formItemList.toArray(formItems);
		setFields(formItems);
		
	}
	
	public class ButtonListItem extends CanvasItem {  
        protected HStack layout;
		public ButtonListItem (IButton[] items) {  
            super();  
            this.layout = new HStack(2);            
            layout.setWidth("100%");              
            super.setEndRow(true);  
            super.setStartRow(true);  
            super.setColSpan("*");  
            super.setShowTitle(false);             
            super.setInitHandler(new InitHandler(layout));
            super.setHeight(25);
            layout.setAlign(Alignment.RIGHT);            
            
            for (int i = 0; i < items.length; i++) {				
            	layout.addMember(items[i]);
			}
        }  
     
		private class InitHandler implements FormItemInitHandler {
			private Layout layout;
			
			public InitHandler(Layout layout){
				this.layout = layout;
			}
			
			@Override
			public void onInit(FormItem item) {
				layout.setWidth("*");                  
                layout.setLeaveScrollbarGap(false);                                         
                ((CanvasItem) item).setCanvas(layout);
				
			}
			
		}
                  
    }


	public void setEntitySelectedListener(EntitySelectedListener selectionListener) {
		this.selectionListener = selectionListener;
	}

	@Override
	public void entitySelected(EntityModel entity) {
		if(selectionListener != null){
			selectionListener.entitySelected(entity);
		}		
	}

	

	  
}
